package com.becca.cake.service.system.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.dao.system.AdminDAO;
import com.becca.cake.dao.system.PostDAO;
import com.becca.cake.model.system.AdminDO;
import com.becca.cake.model.system.AdminSubscriberSettingDO;
import com.becca.cake.service.system.IAdminsService;
import com.becca.cake.util.JsonUtil;
import com.becca.cake.util.MD5Util;
import com.becca.cake.util.StringUtil;

/**
 * 员工管理
 * @author hejinguo
 * @version $Id: AdminsServiceImpl.java, v 0.1 2013-12-2 上午03:32:03
 */
@Service
public class AdminsServiceImpl implements IAdminsService {
    @Autowired
    private AdminDAO adminDAO;
    @Autowired
    private PostDAO  postDAO;

    @Override
    public List<AdminDO> selectAllAdmin(Map<String, Object> map) throws Exception {
        return this.adminDAO.selectAllAdmin(map);
    }

    @Override
    public Long selectAllAdminCount(Map<String, Object> map) throws Exception {
        return this.adminDAO.selectAllAdminCount(map);
    }

    @Override
    public List<AdminDO> selectPersonnelByRoleId(Map<String, Object> map) throws Exception {
        return this.adminDAO.selectPersonnelByRoleId(map);
    }

    @Override
    public Long selectPersonnelByRoleIdCount(Map<String, Object> map) throws Exception {
        return this.adminDAO.selectPersonnelByRoleIdCount(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insertOrUpdateAdmin(AdminDO adminDO) throws Exception {
        if (adminDO.getId() == null) {//新增
            adminDO.setPass(MD5Util.getMD5Password(adminDO.getPass()));
            this.adminDAO.insertAdmin(adminDO);
        } else {//修改
            //删除历史员工岗位信息
            this.postDAO.deletePostById(adminDO.getId());
            this.adminDAO.updateAdmin(adminDO);
        }
        //添加员工岗位信息
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", adminDO.getId());
        map.put("postIdStr", adminDO.getPostId().split(","));
        map.put("createdMan", adminDO.getCreatedMan());
        this.postDAO.insertAdminPost(map);
    }

    @Override
    public String selectAdminById(String adminId) throws Exception {
        AdminDO adminDO = this.adminDAO.selectAdminById(adminId);
        return JsonUtil.writeObjectFormJSON(adminDO);
    }

    @Override
    public String selectAdminNameExist(String adminName) throws Exception {
        String name = this.adminDAO.selectAdminNameExist(adminName);
        if (StringUtil.isNotEmpty(name)) {
            return JsonUtil.returnJsonUnSuccess("用户名称已存在!");
        } else {
            return JsonUtil.returnJsonSuccess("该用户名可以使用!");
        }
    }

    @Override
    public String selectAdminByFunctionId(String functionId) throws Exception {
        List<AdminDO> listPri = this.adminDAO.selectAdminByFunctionId(functionId);
        return JsonUtil.writeObjectNoPageJSON(listPri);
    }

    @Override
    public void updatePriAdminSubscription(Map<String, Object> param) throws Exception {
        this.adminDAO.updatePriAdminSubscription(param);
    }

    @Override
    public List<AdminSubscriberSettingDO> selectUserSubscript(Map<String, Object> param)
                                                                                        throws Exception {
        return this.adminDAO.selectUserSubscript(param);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void addUserSubscript(Map<String, Object> param) throws Exception {
        Integer userId = Integer.valueOf(String.valueOf(param.get("userId")));
        //根据员工ID删除员工提醒信息
        this.adminDAO.deleteUserSubscriptByAdminId(userId);
        //添加员工提醒信息
        this.adminDAO.insertUserSubscript(param);
    }

}
